package com.cn.view;

import com.cn.bean.Department;
import com.cn.dao.DepartmentDao;
import com.cn.dao.WorkerDao;
import com.cn.util.StringUtils;

import javax.swing.*;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Vector;

/**
 * @Author Zhou Tao
 * @Date 2023/12/6 22:02
 */
public class DepartmentManageFrame extends JInternalFrame {
    private JTable departmentTable;
    private JTextArea deptDescTextArea;
    private JTextField departmentNameTextField;
    private JTextField deptIdTextField;
    private JTextField deptNameTextField;

    public DepartmentManageFrame(){
        setClosable(true);
        setIconifiable(true);
        setTitle("部门信息管理界面");
        setBounds(100, 100, 507, 481);
        JScrollPane scrollPane = new JScrollPane();
        JLabel label = new JLabel("请填部门信息");
        departmentNameTextField = new JTextField();
        departmentNameTextField.setColumns(10);

        // 点击"查询"按钮执行search()方法
        JButton searchButton = new JButton("查询");
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                search();
            }
        });
        searchButton.setIcon(new ImageIcon(DepartmentManageFrame.class.getResource("/images/search.png")));

        JPanel panel = new JPanel();
        panel.setBorder(new TitledBorder(null, "操作", TitledBorder.LEADING, TitledBorder.TOP, null, null));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        GroupLayout.ParallelGroup parallelGroup1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequentialGroup1 = groupLayout.createSequentialGroup();
        sequentialGroup1.addGap(42);
        GroupLayout.ParallelGroup parallelGroup2 = groupLayout.createParallelGroup(GroupLayout.Alignment.TRAILING);
        parallelGroup2.addComponent(panel, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE);
        GroupLayout.SequentialGroup sequentialGroup2 = groupLayout.createSequentialGroup();
        sequentialGroup2.addComponent(label);
        sequentialGroup2.addGap(18);
        sequentialGroup2.addComponent(departmentNameTextField, GroupLayout.PREFERRED_SIZE, 133, GroupLayout.PREFERRED_SIZE);
        sequentialGroup2.addGap(18);
        sequentialGroup2.addComponent(searchButton);
        parallelGroup2.addGroup(GroupLayout.Alignment.LEADING,sequentialGroup2);
        parallelGroup2.addComponent(scrollPane, GroupLayout.Alignment.LEADING, GroupLayout.DEFAULT_SIZE, 401, Short.MAX_VALUE);
        sequentialGroup1.addGroup(parallelGroup2);
        sequentialGroup1.addGap(48);
        parallelGroup1.addGroup(sequentialGroup1);
        groupLayout.setHorizontalGroup(parallelGroup1);
        GroupLayout.ParallelGroup createParallelGroup1 = groupLayout.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup createSequentialGroup1 = groupLayout.createSequentialGroup();
        createSequentialGroup1.addGap(33);
        GroupLayout.ParallelGroup createParallelGroup2 = groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE);
        createParallelGroup2.addComponent(label);
        createParallelGroup2.addComponent(departmentNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        createParallelGroup2.addComponent(searchButton);
        createSequentialGroup1.addGroup(createParallelGroup2);
        createSequentialGroup1.addGap(39);
        createSequentialGroup1.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 126, GroupLayout.PREFERRED_SIZE);
        createSequentialGroup1.addGap(18);
        createSequentialGroup1.addComponent(panel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        createSequentialGroup1.addContainerGap(25, Short.MAX_VALUE);
        createParallelGroup1.addGroup(createSequentialGroup1);
        groupLayout.setVerticalGroup(createParallelGroup1);

        JLabel lblNewLabel = new JLabel("编号：");
        deptIdTextField = new JTextField();
        deptIdTextField.setEditable(false);
        deptIdTextField.setColumns(10);
        JLabel label_1 = new JLabel("部门名称信息");
        deptNameTextField = new JTextField();
        deptNameTextField.setColumns(10);
        JLabel lblNewLabel_1 = new JLabel("描述：");
        deptDescTextArea = new JTextArea();
        deptDescTextArea.setLineWrap(true);
        deptDescTextArea.setWrapStyleWord(true);

        // 点击"修改"按钮执行update()方法
        JButton updateButton = new JButton("修改");
        updateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                update();
            }
        });
        updateButton.setIcon(new ImageIcon(DepartmentManageFrame.class.getResource("/images/modify.png")));

        // 点击"删除"按钮执行delete()方法
        JButton deleteButton = new JButton("删除");
        deleteButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                delete();
            }
        });
        deleteButton.setIcon(new ImageIcon(DepartmentManageFrame.class.getResource("/images/delete.png")));
        GroupLayout gl_panel = new GroupLayout(panel);
        GroupLayout.ParallelGroup paraGroup1 = gl_panel.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup sequGroup1 = gl_panel.createSequentialGroup();
        sequGroup1.addContainerGap();
        GroupLayout.ParallelGroup paraGroup2 = gl_panel.createParallelGroup(GroupLayout.Alignment.LEADING, false);
        GroupLayout.SequentialGroup sequGroup2 = gl_panel.createSequentialGroup();
        sequGroup2.addComponent(lblNewLabel);
        sequGroup2.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        sequGroup2.addComponent(deptIdTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        sequGroup2.addGap(31);
        sequGroup2.addComponent(label_1);
        sequGroup2.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED);
        sequGroup2.addComponent(deptNameTextField, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE);
        paraGroup2.addGroup(sequGroup2);
        GroupLayout.SequentialGroup sequGroup3 = gl_panel.createSequentialGroup();
        sequGroup3.addComponent(lblNewLabel_1);
        sequGroup3.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED);
        sequGroup3.addComponent(deptDescTextArea);
        paraGroup2.addGroup(sequGroup3);
        GroupLayout.SequentialGroup sequGroup4 = gl_panel.createSequentialGroup();
        sequGroup4.addComponent(updateButton);
        sequGroup4.addGap(26);
        sequGroup4.addComponent(deleteButton);
        paraGroup2.addGroup(sequGroup4);
        sequGroup1.addGroup(paraGroup2);
        sequGroup1.addContainerGap(37, Short.MAX_VALUE);
        paraGroup1.addGroup(sequGroup1);
        gl_panel.setHorizontalGroup(paraGroup1);
        GroupLayout.ParallelGroup pGroup1 = gl_panel.createParallelGroup(GroupLayout.Alignment.LEADING);
        GroupLayout.SequentialGroup group1 = gl_panel.createSequentialGroup();
        group1.addContainerGap();
        GroupLayout.ParallelGroup pGroup2 = gl_panel.createParallelGroup(GroupLayout.Alignment.BASELINE);
        pGroup2.addComponent(lblNewLabel);
        pGroup2.addComponent(deptIdTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        pGroup2.addComponent(label_1);
        pGroup2.addComponent(deptNameTextField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE);
        group1.addGroup(pGroup2);
        group1.addGap(18);
        GroupLayout.ParallelGroup pGroup3 = gl_panel.createParallelGroup(GroupLayout.Alignment.LEADING);
        pGroup3.addComponent(lblNewLabel_1);
        pGroup3.addComponent(deptDescTextArea, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE);
        group1.addGroup(pGroup3);
        group1.addGap(18);
        GroupLayout.ParallelGroup pGroup4 = gl_panel.createParallelGroup(GroupLayout.Alignment.BASELINE);
        pGroup4.addComponent(updateButton);
        pGroup4.addComponent(deleteButton);
        group1.addGroup(pGroup4);
        group1.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE);
        pGroup1.addGroup(group1);
        gl_panel.setVerticalGroup(pGroup1);
        panel.setLayout(gl_panel);

        // 点击部门列表中的某个部门执行click()方法
        departmentTable = new JTable();
        departmentTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                click();
            }
        });
        departmentTable.setModel(new DefaultTableModel(
                new Object[][] {},
                new String[] {"编号", "部门名称信息", "部门描述信息"}
        ) {
            boolean[] columnEditables = new boolean[] {false, false, false};
            public boolean isCellEditable(int row, int column) {
                return columnEditables[column];
            }
        });
        departmentTable.getColumnModel().getColumn(1).setPreferredWidth(110);
        departmentTable.getColumnModel().getColumn(2).setPreferredWidth(123);
        scrollPane.setViewportView(departmentTable);
        getContentPane().setLayout(groupLayout);
        // 初始化显示部门的表格
        this.initSchoolClassTable(null);
        deptDescTextArea.setBorder(new LineBorder(new java.awt.Color(127,157,185), 1, false));
    }

    /**
     * core code
     * 初始化部门表格
     */
    private void initSchoolClassTable(String departmentName){
        DefaultTableModel tableModel=(DefaultTableModel) departmentTable.getModel();
        tableModel.setRowCount(0);
        // 利用departmentDao查询部门
        DepartmentDao departmentDao=new DepartmentDao();
        ArrayList<Department> list=departmentDao.query(departmentName);
        for(int i=0;i<list.size();i++){
            Department dept = list.get(i);
            Vector v=new Vector();
            v.add(dept.getDeptId());
            v.add(dept.getDeptName());
            v.add(dept.getDeptDesc());
            tableModel.addRow(v);
        }
    }

    /**
     * core code
     * 处理部门表格行点击事件
     */
    private void click() {
        int row=departmentTable.getSelectedRow();
        String classID = departmentTable.getValueAt(row, 0)+"";
        deptIdTextField.setText(classID);
        String deptName = (String)departmentTable.getValueAt(row, 1);
        deptNameTextField.setText(deptName);
        String deptDesc = (String)departmentTable.getValueAt(row, 2);
        deptDescTextArea.setText(deptDesc);
    }

    /**
     * core code
     * 查询部门
     */
    private void search() {
        // 获取查询条件
        String departmentName=this.departmentNameTextField.getText();
        // 依据条件初始化部门表格
        this.initSchoolClassTable(departmentName);
    }

    /**
     * core code
     * 删除部门
     */
    private void delete() {
        String id= deptIdTextField.getText();
        if(StringUtils.isEmpty(id)){
            JOptionPane.showMessageDialog(null, "请选择要删除的记录");
            return;
        }
        int select=JOptionPane.showConfirmDialog(null, "确定要删除该记录吗？");
        // 确认删除
        if(select == JOptionPane.YES_OPTION){
            // 利用WorkerDao判断该部门中是否存在员工
            WorkerDao workerDao=new WorkerDao();
            boolean isHaveStudentInClass=workerDao.isHaveStudentInClass(Integer.parseInt(id));
            if(isHaveStudentInClass){
                JOptionPane.showMessageDialog(null, "当前部门中有员工，不能删除该部门");
                return;
            }

            // 利用departmentDao删除部门
            DepartmentDao departmentDao=new DepartmentDao();
            int result=departmentDao.delete(Integer.parseInt(id));
            if(result==1){
                JOptionPane.showMessageDialog(null, "删除成功");
                this.reset();
                this.initSchoolClassTable(null);
            }else{
                JOptionPane.showMessageDialog(null, "删除失败");
            }
        }
    }

    /**
     * core code
     * 修改部门
     */
    private void update() {
        String id= deptIdTextField.getText();
        String deptName= deptNameTextField.getText();
        String deptDesc= deptDescTextArea.getText();
        if(StringUtils.isEmpty(id)){
            JOptionPane.showMessageDialog(null, "请选择要修改的记录");
            return;
        }
        if(StringUtils.isEmpty(deptName)){
            JOptionPane.showMessageDialog(null, "部门信息名称不能为空");
            return;
        }

        // 利用departmentDao修改部门
        DepartmentDao departmentDao=new DepartmentDao();
        Department department=new Department(Integer.parseInt(id),deptName,deptDesc);
        int result=departmentDao.update(department);
        if(result==1){
            JOptionPane.showMessageDialog(null, "修改成功");
            // 重置所填数据
            this.reset();
            // 初始化部门表格
            this.initSchoolClassTable(null);
        }else{
            JOptionPane.showMessageDialog(null, "修改失败");
        }

    }

    /**
     * core code
     * 重置
     */
    private void reset(){
        this.deptIdTextField.setText("");
        this.deptNameTextField.setText("");
        this.deptDescTextArea.setText("");
    }
}
